﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace GPSTrackingDBCustom.Log
{
    public class MessageQueueLog
    {
        enum Category
        {
            Information = 0,
            Warning = 1,
            Error = 2
        }

        private static string GetCallingMethod(StackFrame frame)
        {
            var method = frame.GetMethod();
            return method.DeclaringType.FullName + "." + method.Name;
        }

        /// <summary>
        /// Logs an informational message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Info(string message, string gpsModel)
        {
            GPSTrackingDBV2.Entities.MessageQueueLog LogEntity = new GPSTrackingDBV2.Entities.MessageQueueLog();
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Information.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;            

            GPSTrackingDBV2.Data.DataRepository.MessageQueueLogProvider.Insert(LogEntity);

        }

        /// <summary>
        /// Logs a warning message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Warning(string message, string gpsModel)
        {
            GPSTrackingDBV2.Entities.MessageQueueLog LogEntity = new GPSTrackingDBV2.Entities.MessageQueueLog();
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Warning.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;

            GPSTrackingDBV2.Data.DataRepository.MessageQueueLogProvider.Insert(LogEntity);
        }

        /// <summary>
        /// Logs an error message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Error(string message, string gpsModel)
        {
            GPSTrackingDBV2.Entities.MessageQueueLog LogEntity = new GPSTrackingDBV2.Entities.MessageQueueLog();
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Error.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;

            GPSTrackingDBV2.Data.DataRepository.MessageQueueLogProvider.Insert(LogEntity);
        }
    }
}
